#include "isd1420.h"

void initisd1420(u8 *mode){
    outportb(CRL_ISD1420, 0x07);
    *mode = 0x07;
    outportb(ADDR_ISD1420, 0x00);
}

void stopISD(u8 *mode){
    u8 temp = *mode & 0xfb;
    outportb(CRL_ISD1420, temp);
    temp |= 0x04;
    outportb(CRL_ISD1420, temp);
    temp |= 0x03;
    outportb(CRL_ISD1420, temp);
    *mode = temp;
    outportb(ADDR_ISD1420, 0x00);
    delay(50);
    delay(50);
    delay(50);
}

void recISD(u8 *mode, u8 startAddr){
    u8 temp;
    outportb(ADDR_ISD1420, startAddr);
    temp = *mode & 0xfe;
    outportb(CRL_ISD1420, temp);
    *mode = temp;
}

void playISD(u8 *mode, u8 startAddr){
    u8 temp;
    stopISD(mode);
    outportb(ADDR_ISD1420, startAddr);
    temp = *mode & 0xfd;
    outportb(CRL_ISD1420, temp);
    temp |= 0x02;
    outportb(CRL_ISD1420, temp);
    *mode = temp;
}

void recSubProgram(u8 *mode, u8 startAddr){
    recISD(mode, startAddr);
    delay(1000);
    stopISD(mode);
}

void playSubProgram(u8 *mode, u8 startAddr){
    playISD(mode, startAddr);
    delay(1000);
    stopISD(mode);
}

void scanVoicePlayFlag(u8 *mode, u8 *flag, u8 hour){
    u8 high = hour / 10;
    u8 low = hour % 10;
    if(*flag){
        if(high == 0){
            switch (low){
            case 0:
                playSubProgram(mode, ADDR_0);
                playSubProgram(mode, ADDR_TIP);
                *flag = 0;
                break;
            case 1:
                playSubProgram(mode, ADDR_1);
                playSubProgram(mode, ADDR_TIP);
                *flag = 0;
                break;
            case 2:
                playSubProgram(mode, ADDR_2);
                playSubProgram(mode, ADDR_TIP);
                *flag = 0;
                break;
            case 3:
                playSubProgram(mode, ADDR_3);
                playSubProgram(mode, ADDR_TIP);
                *flag = 0;
                break;
            case 4:
                playSubProgram(mode, ADDR_4);
                playSubProgram(mode, ADDR_TIP);
                *flag = 0;
                break;
            case 5:
                playSubProgram(mode, ADDR_5);
                playSubProgram(mode, ADDR_TIP);
                *flag = 0;
                break;
            case 6:
                playSubProgram(mode, ADDR_6);
                playSubProgram(mode, ADDR_TIP);
                *flag = 0;
                break;
            case 7:
                playSubProgram(mode, ADDR_7);
                playSubProgram(mode, ADDR_TIP);
                *flag = 0;
                break;
            case 8:
                playSubProgram(mode, ADDR_8);
                playSubProgram(mode, ADDR_TIP);
                *flag = 0;
                break;
            case 9:
                playSubProgram(mode, ADDR_9);
                playSubProgram(mode, ADDR_TIP);
                *flag = 0;
                break;
            default:
                break;
            }
        }
        else if(high == 1){
            playSubProgram(mode, ADDR_10);
            switch (low){
            case 0:
                playSubProgram(mode, ADDR_TIP);
                *flag = 0;
                break;
            case 1:
                playSubProgram(mode, ADDR_1);
                playSubProgram(mode, ADDR_TIP);
                *flag = 0;
                break;
            case 2:
                playSubProgram(mode, ADDR_2);
                playSubProgram(mode, ADDR_TIP);
                *flag = 0;
                break;
            case 3:
                playSubProgram(mode, ADDR_3);
                playSubProgram(mode, ADDR_TIP);
                *flag = 0;
                break;
            case 4:
                playSubProgram(mode, ADDR_4);
                playSubProgram(mode, ADDR_TIP);
                *flag = 0;
                break;
            case 5:
                playSubProgram(mode, ADDR_5);
                playSubProgram(mode, ADDR_TIP);
                *flag = 0;
                break;
            case 6:
                playSubProgram(mode, ADDR_6);
                playSubProgram(mode, ADDR_TIP);
                *flag = 0;
                break;
            case 7:
                playSubProgram(mode, ADDR_7);
                playSubProgram(mode, ADDR_TIP);
                *flag = 0;
                break;
            case 8:
                playSubProgram(mode, ADDR_8);
                playSubProgram(mode, ADDR_TIP);
                *flag = 0;
                break;
            case 9:
                playSubProgram(mode, ADDR_9);
                playSubProgram(mode, ADDR_TIP);
                *flag = 0;
                break;
            default:
                break;
            }
        }
        else if(high == 2){
            playSubProgram(mode, ADDR_2);
            playSubProgram(mode, ADDR_10);
            switch (low){
            case 0:
                playSubProgram(mode, ADDR_TIP);
                *flag = 0;
                break;
            case 1:
                playSubProgram(mode, ADDR_1);
                playSubProgram(mode, ADDR_TIP);
                *flag = 0;
                break;
            case 2:
                playSubProgram(mode, ADDR_2);
                playSubProgram(mode, ADDR_TIP);
                *flag = 0;
                break;
            case 3:
                playSubProgram(mode, ADDR_3);
                playSubProgram(mode, ADDR_TIP);
                *flag = 0;
                break;
            default:
                break;
            }
        }
    }
}